﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VacationDaysCommons;
using System.Collections.ObjectModel;
using VacationDaysDatabase;

namespace VacationDaysClient.CustomControls
{
    /// <summary>
    /// Interaction logic for VacationDaysList.xaml
    /// </summary>
    public partial class UserVacationDaysList : UserControl
    {

        public event EventHandler NewRequestClicked;

        void FireNewRequestClicked()
        {
            if (NewRequestClicked != null)
            {
                NewRequestClicked(this, null);
            }
        }

        public ObservableCollection<VDVacationRequest> _employeeRequestsCollection = new ObservableCollection<VDVacationRequest>();
        public ObservableCollection<VDVacationRequest> EmployeeRequestsCollection { get { return _employeeRequestsCollection; } }

        private Guid userId;

        public Guid UserId {
            set {userId = value;
                 RefreshVacationDays();
            }
        }

        private void RefreshVacationDays()
        {
            using (VDRequestServiceClient vr = new VDRequestServiceClient())
            {
                var vacationDayRequests = vr.GetEmployeeRequests(userId);
                _employeeRequestsCollection.Clear();
                foreach (var vacationRequest in vacationDayRequests)
                {
                    _employeeRequestsCollection.Add(vacationRequest);
                }
            }
        }

        public void RefreshContent()
        {
            RefreshVacationDays();
        }

        public UserVacationDaysList()
        {
            InitializeComponent();
        }

        public UserVacationDaysList(Guid currentUserId)
        {
            InitializeComponent();
            UserId = currentUserId;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(string.Format("cancel Request {0}", ((VDVacationRequest)listView1.SelectedItem).Context));

            var selectedRequest = (VDVacationRequest)listView1.SelectedItem;

            if (selectedRequest.RequestState == VDRequestState.Pending ||
                selectedRequest.RequestState == VDRequestState.Rejected)
            {
                // cancel request here
 
                using (VDRequestServiceClient vr = new VDRequestServiceClient())
                {
                    var reqList = new List<int>();
                    reqList.Add(selectedRequest.Id);
                    if (vr.CancelVacationRequests(reqList.ToArray()).Contains(selectedRequest.Id))
                    {
                        MessageBox.Show("Vacation request canceled!");
                        RefreshVacationDays();
                    }
                }

            }
            else
            {
                MessageBox.Show("Only Pending and Rejected requests can be cancelled!");
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            FireNewRequestClicked();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            RefreshContent();
        }


        private void CancelMultiple(List<int> requests)
        {

            if (requests.Count > 0)
            { // execute only if there are items to handle
                using (VDRequestServiceClient vr = new VDRequestServiceClient())
                {
                    var updatedRequests = vr.CancelVacationRequests(requests.ToArray());
                }
                RefreshContent();
            }

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            button2.ContextMenu.IsOpen = true;
            return;
        }

        private void CancelSelected_Click(object sender, RoutedEventArgs e)
        {
            var requestList = listView1.SelectedItems.AsQueryable().OfType<VDVacationRequest>().
                                    Where(req => req.RequestState == VDRequestState.Rejected || req.RequestState == VDRequestState.Pending).
                                    Select(req => req.Id).ToList();
            if (requestList.Count < 1)
            {
                MessageBox.Show("Only Pending and Rejected requests can be cancelled!");
            }
            else
            {
                CancelMultiple(requestList);
            }
        }

        private void CancelPending_Click(object sender, RoutedEventArgs e)
        {
            var requestList = listView1.ItemsSource.AsQueryable().OfType<VDVacationRequest>().
                                    Where(req => req.RequestState == VDRequestState.Pending).
                                    Select(req => req.Id).ToList();
            if (requestList.Count < 1)
            {
                MessageBox.Show("There are no Pending requests to be cancelled!");
            }
            else
            {
                CancelMultiple(requestList);
            }
        }

        private void CancelRejected_Click(object sender, RoutedEventArgs e)
        {
            var requestList = listView1.ItemsSource.AsQueryable().OfType<VDVacationRequest>().
                                    Where(req => req.RequestState == VDRequestState.Rejected).
                                    Select(req => req.Id).ToList();
            if (requestList.Count < 1)
            {
                MessageBox.Show("There are no Rejected requests to be cancelled!");
            }
            else
            {
                CancelMultiple(requestList);
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listView1.SelectedItem!= null)
            {
                MessageBox.Show("Request editing feature coming soon ;)");
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
        }


    }
}
